package com.zxm.web.controller.txcloud.ocr;

import com.zxm.common.core.domain.AjaxResult;
import com.zxm.common.core.domain.TxyFactory;
import com.zxm.common.core.domain.entity.TxyCarOcr;
import com.zxm.common.utils.StringUtils;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author silence.Z
 * @Date 2022/6/9 16:17
 * @PackageName:com.zxm.web.controller.txcloud.ocr
 * @ClassName: CarController
 * @Description: 汽车场景识别
 * @Version 1.0
 */
@RestController
@RequestMapping("txcloud/ocr/car")
public class CarController {
	private static final Logger log = LoggerFactory.getLogger(CarController.class);


	@PreAuthorize("@ss.hasPermi('txcloud:ocr:car:list')")
	@PostMapping("driverLicenseOCR")
	/**
	 * 驾驶证识别
	 */
	public AjaxResult driverLicenseOCR(@Validated @RequestBody TxyCarOcr carOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		DriverLicenseOCRRequest req = new DriverLicenseOCRRequest();
		if (StringUtils.isNotEmpty(carOcr.getImageBase64())){
			req.setImageBase64(carOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(carOcr.getImageUrl())){
			req.setImageUrl(carOcr.getImageUrl());
		}
		if (StringUtils.isNotEmpty(carOcr.getCardSide())){
			req.setCardSide(carOcr.getCardSide());
		}
		DriverLicenseOCRResponse resp = client.DriverLicenseOCR(req);
		// 输出json格式的字符串回包
		log.info("驾驶证（正、副页）："+DriverLicenseOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);

	}
	@PreAuthorize("@ss.hasPermi('txcloud:ocr:car:list')")
	@PostMapping("vehicleLicenseOCR")
	/**
	 * 行驶证识别
	 */
	public AjaxResult vehicleLicenseOCR(@Validated @RequestBody TxyCarOcr carOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		// 实例化一个请求对象,每个接口都会对应一个request对象
		VehicleLicenseOCRRequest req = new VehicleLicenseOCRRequest();
		if (StringUtils.isNotEmpty(carOcr.getImageBase64())){
			req.setImageBase64(carOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(carOcr.getImageUrl())){
			req.setImageUrl(carOcr.getImageUrl());
		}
		if (StringUtils.isNotEmpty(carOcr.getCardSide())){
			req.setCardSide(carOcr.getCardSide());
		}

		// 返回的resp是一个VehicleLicenseOCRResponse的实例，与请求对象对应
		VehicleLicenseOCRResponse resp = client.VehicleLicenseOCR(req);
		// 输出json格式的字符串回包
		log.info(VehicleLicenseOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}

	@PreAuthorize("@ss.hasPermi('txcloud:ocr:car:list')")
	@PostMapping("licensePlateOCR")
	/**
	 * 车牌识别
	 */
	public AjaxResult licensePlateOCR(@Validated @RequestBody TxyCarOcr carOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		// 实例化一个请求对象,每个接口都会对应一个request对象
		LicensePlateOCRRequest req = new LicensePlateOCRRequest();
		if (StringUtils.isNotEmpty(carOcr.getImageBase64())){
			req.setImageBase64(carOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(carOcr.getImageUrl())){
			req.setImageUrl(carOcr.getImageUrl());
		}
		// 返回的resp是一个VehicleLicenseOCRResponse的实例，与请求对象对应
		LicensePlateOCRResponse resp = client.LicensePlateOCR(req);
		// 输出json格式的字符串回包
		log.info(VehicleLicenseOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}
}
